package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.UserFollow;
import com.mapper.UserFollowDao;
import com.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowDao, UserFollow> implements UserFollowService {

    @Autowired
    private UserFollowDao userFollowDao;

    @Override
    @Transactional
    public void addUserFollow(UserFollow userFollow) {
        // 删除旧的信息
        QueryWrapper<UserFollow> userFollowQueryWrapper = new QueryWrapper<>();
        userFollowQueryWrapper.lambda().eq(UserFollow::getUserId, userFollow.getUserId())
                .eq(UserFollow::getFollowId, userFollow.getFollowId());
        userFollowDao.delete(userFollowQueryWrapper);
        // 查询是否互粉
        QueryWrapper<UserFollow> userFollowQueryWrapper2 = new QueryWrapper<>();
        userFollowQueryWrapper2.lambda().eq(UserFollow::getUserId, userFollow.getFollowId())
                .eq(UserFollow::getFollowId, userFollow.getUserId());
        UserFollow isFollowEach = userFollowDao.selectOne(userFollowQueryWrapper2);

        userFollow.setCreateTime(new Date());
        if (isFollowEach != null) {
            // 新增关注
            userFollow.setIsFollowEach(1L);
            userFollowDao.insert(userFollow);
            // 修改另一个用户的互粉信息
            isFollowEach.setIsFollowEach(1L);
            userFollowDao.updateById(isFollowEach);
        } else {
            userFollowDao.insert(userFollow);
        }
    }

    @Override
    @Transactional
    public void deleteUserFollow(UserFollow userFollow) {
        QueryWrapper<UserFollow> userFollowQueryWrapper = new QueryWrapper<>();
        userFollowQueryWrapper.lambda().eq(UserFollow::getUserId, userFollow.getUserId())
                .eq(UserFollow::getFollowId, userFollow.getFollowId());
        UserFollow isFollowEach = userFollowDao.selectOne(userFollowQueryWrapper);
        // 删除另一个用户的互粉信息
        if (isFollowEach.getIsFollowEach() == 1L) {
            QueryWrapper<UserFollow> userFollowQueryWrapper2 = new QueryWrapper<>();
            userFollowQueryWrapper2.lambda().eq(UserFollow::getUserId, userFollow.getFollowId())
                    .eq(UserFollow::getFollowId, userFollow.getUserId());
            UserFollow userFollow1 = new UserFollow();
            userFollow1.setIsFollowEach(0L);
            userFollowDao.update(userFollow1, userFollowQueryWrapper2);
        }
        // 删除关注
        userFollowDao.delete(userFollowQueryWrapper);
    }

    @Override
    public List<UserFollow> getUserFollow(Long userId) {
//        QueryWrapper<UserFollow> userFollowQueryWrapper = new QueryWrapper<>();
//        userFollowQueryWrapper.lambda().eq(UserFollow::getUserId, userId);
        List<UserFollow> list = userFollowDao.getUserFollow(userId);
        return list;
    }

    @Override
    public List<UserFollow> getUserFans(Long userId) {
//        QueryWrapper<UserFollow> userFollowQueryWrapper = new QueryWrapper<>();
//        userFollowQueryWrapper.lambda().eq(UserFollow::getFollowId, userId);
        List<UserFollow> fanList = userFollowDao.getUserFans(userId);
        return fanList;
    }

}
